package com.gfxy.modules.educationandservice.controller;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2021/9/27 11:33 上午
 */
@Controller("educationAndServiceExcelView")
public class EducationAndServiceExcelView extends MiniAbstractExcelView {
    public static final String FILE_NAME = "fileName";
    public static final String TITLE = "title";
    public static final String CONTENT = "content";

    public EducationAndServiceExcelView() {
    }
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "临时文件";
        Workbook workbook =  new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        List<String> titles = (List<String>) model.get(TITLE);
        List<List<String>> contents = (List<List<String>>) model.get(CONTENT);

        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        Row row = sheet.createRow(0);
        for (int i = 0; i < titles.size(); i++) {
            Cell cell = row.createCell(i);
            String title = titles.get(i);
            cell.setCellValue(title);
            cell.setCellStyle(style);
        }

        int rowIndex = 1;



        for(int i=0; i< contents.size();i++){
            List<String> content = contents.get(i);
            Row row1 = sheet.createRow(rowIndex);
            Row row2 = sheet.createRow(rowIndex+1);
            for (int i1 = 0; i1 < content.size(); i1++) {
                Cell cell = row1.createCell(i1);
                String title = content.get(i1);
                cell.setCellValue(title);
                cell.setCellStyle(style);

                Cell cell2 = row2.createCell(i1);
                cell2.setCellStyle(style);
            }


            CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex+1, 0, 0);
            CellRangeAddress region2 = new CellRangeAddress(rowIndex, rowIndex+1, 1, 1);
            sheet.addMergedRegion(region);
            sheet.addMergedRegion(region2);
            rowIndex = rowIndex+2;


        }



        if (model.containsKey(FILE_NAME)) {
            codedFileName = (String)model.get(FILE_NAME);
        }

        codedFileName = codedFileName + ".xls";

        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }


        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }




}
